#!/bin/bash

# 安装必要的软件
sudo apt update
sudo apt install -y nginx python3 python3-pip certbot python3-certbot-nginx

# 创建网站目录
sudo mkdir -p /var/www/upme/frontend
sudo mkdir -p /var/www/upme/backend

# 复制前端文件
sudo cp -r ../frontend/* /var/www/upme/frontend/

# 安装后端依赖
cd ../backend
sudo pip3 install -r requirements.txt

# 复制后端文件
sudo cp -r * /var/www/upme/backend/

# 配置Nginx
sudo cp nginx.conf /etc/nginx/sites-available/upme.cool
sudo ln -s /etc/nginx/sites-available/upme.cool /etc/nginx/sites-enabled/
sudo rm -f /etc/nginx/sites-enabled/default

# 检查Nginx配置
sudo nginx -t

# 重启Nginx
sudo systemctl restart nginx

# 配置后端服务
sudo tee /etc/systemd/system/upme.service << EOF
[Unit]
Description=UpMe Backend Service
After=network.target

[Service]
User=www-data
WorkingDirectory=/var/www/upme/backend
ExecStart=/usr/bin/python3 chat_api.py
Restart=always

[Install]
WantedBy=multi-user.target
EOF

# 启动后端服务
sudo systemctl daemon-reload
sudo systemctl enable upme
sudo systemctl start upme

# 申请SSL证书（取消注释以启用HTTPS）
# sudo certbot --nginx -d upme.cool -d www.upme.cool

echo "部署完成！"
echo "请确保已经配置了域名解析，将 upme.cool 和 www.upme.cool 指向服务器IP"
echo "如需启用HTTPS，请取消deploy.sh中关于certbot的注释并重新运行" 